package com.metaverse.backend.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Slf4j
@Converter
public class JSONObjectConverter implements AttributeConverter<JSONObject, String> {
    @Override
    public String convertToDatabaseColumn(JSONObject jsonObject) {
        if (jsonObject != null) {
            return jsonObject.toJSONString();
        }
        return null;
    }

    @Override
    public JSONObject convertToEntityAttribute(String s) {
        try {
            return JSON.parseObject(s);
        } catch (Exception e) {
            log.error("parse json error", e);
        }
        return null;
    }
}
